package jay.listitem;



import java.util.List;

import com.vtc.list.AlertDialog;
import com.vtc.list.DialogInterface;
import com.vtc.list.Message;
import com.vtc.list.PointDTO;
import com.vtc.list.R;





import android.app.LauncherActivity.ListItem;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class Adapter extends ArrayAdapter<ItemDTO>{
	private List<ItemDTO> items;
	private Handler deleteHandler;
	
	public List<ItemDTO> getItems() {
		return items;
	}

	public void setItems(List<ItemDTO> items) {
		this.items = items;
	}

	private Context context;

	public Adapter(Context context, int textViewResourceId,List<ItemDTO> items,Handler hl) {
		super(context, textViewResourceId,items);
		this.items=items;
		this.context=context;
		this.deleteHandler=hl;	
		// TODO Auto-generated constructor stub
	}
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater infalInflater = (LayoutInflater) getContext()
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			v = infalInflater.inflate(R.layout.item, null);
		}
		final ItemDTO pointDTO = items.get(position);
		if (pointDTO != null) {
			EditText tensv = (EditText) v.findViewById(R.id.e1);
			tensv.setText("" + pointDTO.getTensv());
			final EditText tenmon = (EditText) v.findViewById(R.id.e2);
			tenmon.setText(pointDTO.getTenmon());
			EditText tendiem =(EditText) v.findViewById(R.id.e3);
			tendiem.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Toast.makeText(getContext(), pointDTO.getDiem(), Toast.LENGTH_LONG).show();
				}
			});

			if(pointDTO.check){
				ImageButton delete = (ImageButton)v.findViewById(R.id.im1);
				delete.setBackgroundColor(Color.RED);
				delete.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						callRemove(pointDTO, items);
					}
				});
			}
		}
		return v;
		
	}
	private void callRemove(final PointDTO point, final List<PointDTO> items) {
		AlertDialog.Builder alert = new AlertDialog.Builder(context);
		alert.setTitle(context.getString(R.string.app_name));
		alert.setMessage("Muon xoa khong?");
		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				Message msg = new Message();
				int position = items.indexOf(point);
				msg.arg1 = position;
				deleteHandler.sendMessage(msg); //
			}
		});
		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				// nothing to do
			}
		});
		alert.show();
	}
	

}
